﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Islamic_influence_in_Venetian_cuture.LangResources;
using Islamic_influence_in_Venetian_cuture.View.UserControls;

namespace Islamic_influence_in_Venetian_cuture.View
{
    public partial class InfluencePivotMenu : PhoneApplicationPage
    {
        public InfluencePivotMenu()
        {
            Lang lang = new Lang();
            DataContext = lang;
            InitializeComponent();
            InitializePivotItems(lang);
        }

        private void InitializePivotItems(Lang lang)
        {
            var props = typeof(Lang).GetProperties().Where(p => p.Name.StartsWith("ArtInfluence")).ToArray();
            for (int i = 0; i < props.Length; i++)
            {
                AddPivotItem(props[i].Name, i);
            }
            //foreach (var prop in props)
            //{
            //    //var name = prop.GetGetMethod().Invoke(lang, null) as string;
            //    AddPivotItem(prop.Name);
            //}
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            foreach (var item in PivotContainer.Items)
            {
                var list = ((item as PivotItem).Content as InfluenceMenuPivotElement).Arguments;
                list.SelectedItem = null;
                //list.SelectedItems.Clear();
            }
            base.OnNavigatedTo(e);
        }

        private void AddPivotItem(string name, int index)
        {
            var to_add = new PivotItem();
            var binding = new System.Windows.Data.Binding(name);

            //to_add.DataContext = DataContext;
            to_add.SetBinding(PivotItem.HeaderProperty, binding);
            
            var pivot_content = new InfluenceMenuPivotElement(index);
            pivot_content.SelectionChanged += new SelectionChangedEventHandler(pivot_content_SelectionChanged);
            to_add.Content = pivot_content;
            PivotContainer.Items.Add(to_add);
        }

        void pivot_content_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/InfluenceContent.xaml", UriKind.Relative));
            //MessageBox.Show((e.AddedItems[0] as UserControls.SuperString).Idx);
            //MessageBox.Show("ciccio");
        }

        #region ApplicationBar
        private void Arabic_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.Arabic;
        }

        private void English_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.English;
        }

        private void Farsi_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.Persian;
        }

        private void Italian_bar_Click(object sender, EventArgs e)
        {
            LangResources.Lang.CurrentCulture = LangResources.Cultures.Italiano;
        }
        #endregion
    }
}
